<?php
/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

namespace org\boapi\shop;

include_once (__DIR__ . '/EAN.php');

/**
 * International Standard Book Number [ISBN] is a 13 digit number since 01.01.2007. Before 01.01.2007
 * the ISBN has 10 digits.  
 * @license Apache License Version 2.0
 * @author Sebastian Ritter
 */
abstract class ISBN implements \org\boapi\core\IChecknumber {
	/**
	 * Create an old ISBN from new ISBN
	 * @param isbn13
	 * @return ISBN10
	 */
    public static function parse ($isbn13) {
      $isbn10 = new ISBN10 ();
      $isbn10->setCompleteValue (substring ($isbn13->getNumber (),3,13));
      $isbn10->setCompleteValue (substring ($isbn10.getNumber (),0, 9) + $isbn10->getChecknumber ());
      return isbn;
    }
    /**
     * Creates a ISBN Object from EAN
     * <param name="ean"></param>
     * @return ISBN instance
     */
    public static function parse ($ean) {
      $isbn13 = new ISBN13 ();
      $isbn13 ->setCompleteValue($ean->getNumber ());
      return $isbn13;
    }
    /**
     * The check digit
     */
    private $checknumber = 'R';
    /** 
     * Get the check digit
     * @return checknumber
     */
    protected function getChecknumber () {
      if ('R' === $this->checknumber ) {
        $this->isChecknumberCorrect ();
      }
      return $this->checknumber;
    }
    /**
     * Set the check digit
     * @param digit
     */
    protected function setChecknumber ($digit) {
      $this->checknumber = digit;
    }

    /**
     * International Standard Book Number
     */
    private $number;
    
    /**
     * The number of ISNN
     * @return ISBN
     */
    public function getNumber () {
      return $this->number;
    }
    /**
     * Set the ISBN number
     * @param number
     */
    protected function setNumber ($number) {
      $this->number = $number;
    }
    /**
     * Is the checknumber correct
     * @return boolean
     */
    abstract public function isChecknumberCorrect ();
    
    /** 
     * Set the ISBN
     * @param isbn
     */
    public function setCompleteValue ($isbn) {
      $nummer ='';
      if (is_array ($isbn)) {
      	$isbn = $isbn [0];
      }
      $isbn = strtoupper($isbn);
      for ($i = 0; $i < strlen($isbn); $i++) {
        $c = substr ($isbn, $i, 1);
        if (is_numeric ($c) || $c == 'X') {
          $nummer = $nummer . $c;
        }
        else if ($c != '-') {
          throw new exception ("Illegal character in ISBN  ($isbn)");
        }
      }
      $this->setNumber($nummer);
      $this.setChecknumber ('R');
    }
}


/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


?>